/* <a-modal
    :title="`店员${model?'编辑':'新增'}`"
    :width="700"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleOk"
    @cancel="handleCancel"
>
</a-modal> */
import pick from 'lodash.pick'
import { isDate, isDateTime, isTime } from '@/utils/util'
import moment from 'moment'
export default {
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      visible: false,
      loading: false,
      form: this.$form.createForm(this),
      model: null
    }
  },
  created () {
    console.log('custom modal created')
    // 防止表单未注册
    this.fields && this.fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', async model => {
      if (model) {
        this.loading = true
        const pickModel = pick(model, this.fields)
        for (const key in pickModel) {
          const value = pickModel[key]
          // 当值为null时,统一数据结构为undefind
          if (value === null) {
            pickModel[key] = undefined
          }
          // 检查model对象是否存在日期格式字符串  YYYY-MM-DD
          if (typeof value === 'string') {
            if (isDate(value)) {
              pickModel[key] = moment(value, 'YYYY-MM-DD')
              console.log('是日期格式')
            }
            if (isTime(value)) {
              pickModel[key] = moment(value, 'HH:mm:ss')
              console.log('是时间格式')
            }
            if (isDateTime(value)) {
              pickModel[key] = moment(value, 'YYYY-MM-DD HH:mm:ss')
              console.log('是日期时间格式')
            }
          }
        }
        this.watchBefore && (await this.watchBefore(model))
        this.form.setFieldsValue(pickModel)
        this.loading = false
        // console.log(this.form.getFieldsValue())
      } else {
        this.form.resetFields()
      }
    })
  },
  methods: {
    show (record = null) {
      this.model = record && { ...record }
      this.visible = true
    },
    handleCancel () {
      this.model = null
      this.visible = false
      this.$emit('cancel')
    },
    handleOk () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true
          for (const key in values) {
            const value = values[key]
            if (value && value._isAMomentObject) {
              // 将自带格式化参数改为24小时,默认为12小时
              console.log(value._i)
              values[key] = value.format(value._f || 'YYYY-MM-DD HH:mm:ss')
            }
          }
          const processParams = (this.handleOkBefore && this.handleOkBefore(values)) || { ...values }
          const action = this.model ? this.editAction : this.addAction
          action(processParams)
            .then(res => {
              this.$message.success('操作成功')
              this.$emit('ok')
              this.visible = false
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
